package com.suxin.plugin.intergration.manager;

import com.suxin.plugin.intergration.manager.finder.PluginDescriptor;
import com.suxin.plugin.intergration.manager.plugin.AbstractPluginManager;
import com.suxin.plugin.intergration.manager.plugin.PluginManager;
import com.suxin.plugin.pf4j.BasePlugin;
import com.suxin.plugin.pf4j.PluginFactory;
import com.suxin.plugin.pf4j.PluginState;
import lombok.Data;

import java.nio.file.Path;

/**
 * @author Tang
 * @classname PluginWrapper
 * @description [ 每个插件实例 ]
 * @date 2022/6/26 23:12
 */
@Data
public class PluginWrapper {

    private PluginManager pluginManager;

    private Path pluginPath;

    private ClassLoader classLoader;

    private PluginDescriptor pluginDescriptor;

    private PluginFactory pluginFactory;

    private PluginState pluginState;

    private BasePlugin plugin;

    public PluginWrapper(Path pluginPath, PluginDescriptor pluginDescriptor, ClassLoader classLoader,
                         PluginManager pluginManager) {
        this.pluginPath = pluginPath;
        this.classLoader = classLoader;
        this.pluginDescriptor = pluginDescriptor;
        this.pluginManager = pluginManager;
        this.pluginState = PluginState.CREATED;
    }

    public BasePlugin getPlugin() {
        if (plugin == null) {
            plugin = pluginFactory.create(this);
        }
        return plugin;
    }
}